import { getUserCenter } from '@/api/user'
import { TOKEN_KEY } from '@/enums/cacheEnums'
import cache from '@/utils/cache'
import { defineStore } from 'pinia'
import { User } from '@/types/user'

interface UserSate {
    userInfo: User
    token: string | null
    temToken: string | null
    loadingTime:number,
    loading: boolean
}
export const useUserStore = defineStore('userStore', {
    state: (): UserSate => ({
        userInfo: {
            sn: '',
            nickname: '请先登陆',
            id: 0,
            avatar: '',
            phone: '',
            create_time: '',
            update_time: '',
            status: 0,
            token: '',
            expire: 0,
            title: '',
            level: 1,
            exp: 0,
            exp_next: 100,
            exp_level: 0
        },
        token: cache.get(TOKEN_KEY) || null,
        temToken: null,
        loadingTime:0,
        loading: false
    }),
    getters: {
        isLogin: (state) => {
            return !!state.token
        }
    },
    actions: {
        async getUser(): Promise<any>{
            return new Promise((resolve, reject) => {

                if(this.userInfo.sn){
                    resolve(this.userInfo)
                    return
                }
                if(this.loading){
                    this.loadingTime=0;
                    const timer = setInterval(() => {
                        if(this.userInfo.sn){
                            clearInterval(timer)
                            resolve(this.userInfo)
                           
                            return
                        }
                        this.loadingTime++
                        if(this.loadingTime > 100){
                            clearInterval(timer)
                            this.loading = false
                            reject('time out')
                            return
                        }
                    }, 100)
                    return
                }
                this.loading = true
                getUserCenter({
                    token: this.token || this.temToken
                }).then((res: any) => {
                    const data = res
                    if(!data.sn){
                        this.loading = false
                        reject('no sn')
                        return
                    }
                    // #ifdef MP-WEIXIN
                    // 微信container 登陆
                    if(!this.token && data.sn){
                        this.login(data.sn)
                    }
                    // #endif
                    this.userInfo = data
                    resolve(data)
                }).catch((err: any) => {
                    console.log(err)
                    reject(err)
                }).finally(() => {
                    this.loading = false
                })    
            })
        },
        login(token: string) {
            this.token = token
            cache.set(TOKEN_KEY, token)
        },
        logout() {
            this.token = ''
            this.userInfo = {}
            cache.remove(TOKEN_KEY)
            this.getUser()
        }
    }
})
